<!--<template>-->
<!--  <div class="app-container">-->
<!--    <el-page-header :content="title" @back="goBack" />-->

<!--    <el-card class="box-card">-->
<!--      <template #header>-->
<!--        <div class="card-header">-->
<!--          <span>学院学科树形结构</span>-->
<!--          <div>-->
<!--            <el-button type="primary" icon="Plus" @click="handleAddCollege">添加学院</el-button>-->
<!--          </div>-->
<!--        </div>-->
<!--      </template>-->

<!--      <SubjectTree ref="subjectTree" />-->
<!--    </el-card>-->
<!--  </div>-->
<!--</template>-->

<!--<script setup>-->
<!--import { ref } from 'vue'-->

<!--import SubjectTree from './SubjectTree.vue'-->

<!--const title = '学科树形结构展示'-->

<!--const subjectTree = ref(null)-->

<!--const goBack = () => {-->
<!--  router.go(-1)-->
<!--}-->

<!--const handleAddCollege = () => {-->
<!--  // 实际项目中这里会跳转到添加学院页面-->
<!--  console.log('添加学院')-->
<!--}-->

<!--// 如果需要暴露方法给父组件-->
<!--defineExpose({-->
<!--  refreshTree: () => {-->
<!--    if (subjectTree.value) {-->
<!--      subjectTree.value.refreshTree()-->
<!--    }-->
<!--  }-->
<!--})-->
<!--</script>-->

<!--<style scoped>-->
<!--.app-container {-->
<!--  padding: 20px;-->
<!--}-->

<!--.box-card {-->
<!--  margin-top: 20px;-->
<!--  height: 100%;-->
<!--}-->

<!--.card-header {-->
<!--  display: flex;-->
<!--  justify-content: space-between;-->
<!--  align-items: center;-->
<!--}-->
<!--</style>-->
<template>
  <div class="app-container">
    <el-page-header :content="title" @back="goBack" />

    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">学院学科树形结构</span>
          <div>
            <el-button type="primary" icon="Plus" @click="handleAddCollege">添加学院</el-button>
          </div>
        </div>
      </template>

      <div class="subject-tree-container">
        <div class="tree-header">
          <div class="search-container">
            <el-input
                v-model="filterText"
                placeholder="输入学院或学科名称搜索"
                clearable
                class="search-input"
            >
              <template #prefix>
                <i class="el-icon-search"></i>
              </template>
            </el-input>
            <el-button type="primary" icon="Refresh" @click="refreshTree">刷新</el-button>
          </div>

          <div class="stats-container">
            <div class="stat-card college-stat">
              <div class="stat-value">{{ stats.collegeCount }}</div>
              <div class="stat-label">学院数量</div>
            </div>
            <div class="stat-card subject-stat">
              <div class="stat-value">{{ stats.firstSubjectCount }}</div>
              <div class="stat-label">一级学科</div>
            </div>
            <div class="stat-card sub-subject-stat">
              <div class="stat-value">{{ stats.secondSubjectCount }}</div>
              <div class="stat-label">二级学科</div>
            </div>
          </div>
        </div>

        <div class="tree-body">
          <el-tree
              ref="treeRef"
              v-loading="loading"
              :data="treeData"
              :props="defaultProps"
              :filter-node-method="filterNode"
              node-key="id"
              default-expand-all
              :expand-on-click-node="false"
              :highlight-current="true"
          >
            <template #default="{ node, data }">
              <div class="custom-tree-node">
                <div class="node-content">
                  <div class="node-label">
                    <i :class="getIcon(data.type)" class="node-icon"></i>
                    <span class="node-text">{{ node.label }}</span>
                    <span :class="['node-badge', getBadgeClass(data.type)]">{{ getBadgeText(data.type) }}</span>
                  </div>
                  <div class="node-info" v-if="data.type === 'firstSubject' || data.type === 'secondSubject'">
                    <span v-if="data.code">代码: {{ data.code }}</span>
                  </div>
                </div>
                <div class="node-actions">
                  <!-- 根据节点类型显示不同的新增按钮 -->
<!--                  <el-button-->
<!--                      v-if="data.type === 'college' || data.type === 'firstSubject'"-->
<!--                      size="small"-->
<!--                      type="success"-->
<!--                      @click.stop="handleAdd(data)"-->
<!--                      class="action-button"-->
<!--                  >-->
<!--                    {{ data.type === 'college' ? '新增一级学科' : '新增二级学科' }}-->
<!--                  </el-button>-->
                  <el-button
                      size="small"
                      type="success"
                      @click.stop="handleAdd(data)"
                      class="action-button"
                  >
                    新增
                  </el-button>
                  <el-button size="small" @click.stop="handleEdit(data)" class="action-button">修改</el-button>
                  <el-button size="small" type="danger" @click.stop="handleDelete(data)" class="action-button">删除</el-button>
                  <el-button size="small" type="info" @click.stop="handleDetail(data)" class="action-button">详情</el-button>
                </div>
              </div>
            </template>
            <template #empty>
              <div class="empty-placeholder">
                <i class="el-icon-files"></i>
                <p>暂无学科数据</p>
              </div>
            </template>
          </el-tree>
        </div>

        <div class="tree-footer">
          <span>数据更新时间: {{ updateTime }}</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
// 注意：这里需要保留 getSubjectTree API 的导入
import { getSubjectTree } from '@/api/college/subjectConstruction'

const router = useRouter()
const title = '学科树形结构展示'

// 原 SubjectTree 组件的逻辑
const filterText = ref('')
const treeRef = ref(null)
const loading = ref(false)
const treeData = ref([])
const updateTime = ref('')
const stats = ref({
  collegeCount: 0,
  firstSubjectCount: 0,
  secondSubjectCount: 0
})

const defaultProps = {
  children: 'children',
  label: 'label'
}

// 获取图标
const getIcon = (type) => {
  switch(type) {
    case 'college': return 'el-icon-office-building college-icon'
    case 'firstSubject': return 'el-icon-notebook-2 subject-icon'
    case 'secondSubject': return 'el-icon-document sub-subject-icon'
    default: return 'el-icon-folder'
  }
}

// 获取徽章文本
const getBadgeText = (type) => {
  switch(type) {
    case 'college': return '学院'
    case 'firstSubject': return '一级学科'
    case 'secondSubject': return '二级学科'
    default: return ''
  }
}

// 获取徽章样式
const getBadgeClass = (type) => {
  switch(type) {
    case 'college': return 'college-badge'
    case 'firstSubject': return 'subject-badge'
    case 'secondSubject': return 'sub-subject-badge'
    default: return ''
  }
}

// 过滤节点
const filterNode = (value, data) => {
  if (!value) return true
  return data.label.toLowerCase().includes(value.toLowerCase()) ||
      (data.code && data.code.toLowerCase().includes(value.toLowerCase()))
}

// 刷新树
const refreshTree = () => {
  loading.value = true
  getSubjectTree().then(response => {
    treeData.value = response.data
    calculateStats()
    updateTime.value = new Date().toLocaleString()
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 计算统计数据
const calculateStats = () => {
  let collegeCount = 0
  let firstSubjectCount = 0
  let secondSubjectCount = 0

  const traverse = (nodes) => {
    nodes.forEach(node => {
      switch(node.type) {
        case 'college':
          collegeCount++
          if (node.children) traverse(node.children)
          break
        case 'firstSubject':
          firstSubjectCount++
          if (node.children) traverse(node.children)
          break
        case 'secondSubject':
          secondSubjectCount++
          break
      }
    })
  }

  traverse(treeData.value)

  stats.value = {
    collegeCount,
    firstSubjectCount,
    secondSubjectCount
  }
}

// 按钮事件处理函数
const handleAdd = (data) => {
  console.log('新增操作:', data)
  // 这里可以添加实际的新增逻辑
}

const handleEdit = (data) => {
  console.log('修改操作:', data)
  // 这里可以添加实际的修改逻辑
}

const handleDelete = (data) => {
  console.log('删除操作:', data)
  // 这里可以添加实际的删除逻辑
}

const handleDetail = (data) => {
  console.log('查看详情:', data)
  // 这里可以添加实际的详情查看逻辑
}

// 父组件方法
const goBack = () => {
  router.go(-1)
}

const handleAddCollege = () => {
  console.log('添加学院')
}

// 监听过滤文本变化
watch(filterText, (val) => {
  treeRef.value.filter(val)
})

onMounted(() => {
  refreshTree()
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.box-card {
  margin-top: 20px;
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
}

.subject-tree-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  overflow: hidden;
}

.tree-header {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f5f7fa;
}

.search-container {
  display: flex;
  margin-bottom: 15px;
  gap: 15px;
}

.search-input {
  flex: 1;
}

.stats-container {
  display: flex;
  justify-content: space-between;
  background: linear-gradient(135deg, #f0f7ff, #e6f7ff);
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.1);
}

.stat-card {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-right: 1px solid #dcdfe6;
}

.stat-card:last-child {
  border-right: none;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 5px;
}

.college-stat .stat-value {
  color: #409EFF;
}

.subject-stat .stat-value {
  color: #67C23A;
}

.sub-subject-stat .stat-value {
  color: #E6A23C;
}

.stat-label {
  font-size: 16px;
  color: #606266;
}

.tree-body {
  flex: 1;
  padding: 20px;
  overflow: auto;
}

.custom-tree-node {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 18px 0;
  border-bottom: 1px solid #f5f7fa;
  min-height: 70px;
}

.node-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.node-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.node-text {
  font-size: 18px;
  font-weight: 500;
}

.node-icon {
  margin-right: 12px;
  font-size: 24px;
}

.college-icon {
  color: #409EFF;
}

.subject-icon {
  color: #67C23A;
}

.sub-subject-icon {
  color: #E6A23C;
}

.node-badge {
  padding: 6px 14px;
  border-radius: 10px;
  font-size: 16px;
  margin-left: 12px;
  font-weight: 500;
}

.college-badge {
  background-color: #ecf5ff;
  color: #409EFF;
}

.subject-badge {
  background-color: #f0f9eb;
  color: #67C23A;
}

.sub-subject-badge {
  background-color: #fdf6ec;
  color: #E6A23C;
}

.node-info {
  font-size: 16px;
  color: #909399;
  margin-left: 40px;
}

.node-actions {
  flex-shrink: 0;
  margin-left: 15px;
  display: flex;
  gap: 8px;
}

.action-button {
  font-size: 14px;
  padding: 8px 12px;
}

.empty-placeholder {
  text-align: center;
  padding: 40px;
  color: #909399;
}

.empty-placeholder i {
  font-size: 56px;
  margin-bottom: 20px;
  color: #dcdfe6;
}

.tree-footer {
  padding: 15px 20px;
  text-align: right;
  font-size: 14px;
  color: #909399;
  border-top: 1px solid #ebeef5;
}

@media (max-width: 768px) {
  .stats-container {
    flex-direction: column;
  }

  .stat-card {
    border-right: none;
    border-bottom: 1px solid #dcdfe6;
  }

  .stat-card:last-child {
    border-bottom: none;
  }

  .node-actions {
    flex-direction: column;
    gap: 6px;
    margin-top: 10px;
  }

  .custom-tree-node {
    flex-direction: column;
    align-items: flex-start;
  }

  .node-actions {
    align-self: flex-end;
  }
}
</style>


